﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VVA.Website.UI.Areas.Admin.Models;

namespace VVA.Website.UI.Areas.Admin.Controllers
{
    [Authorize]
    public class FileManagementController : Controller
    {
        //
        // GET: /Admin/FileManagement/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult FilePicker()
        {
            return PartialView();
        }

        [HttpPost]
        public ActionResult GetFilesInFolder(string path)
        {
            if (Directory.Exists(Server.MapPath(path)))
            {
                var lst = new List<FileInfoModel>();
                var lstFile = Directory.EnumerateFiles(Server.MapPath(path));

                var appPath = Server.MapPath("~/");

                for (int i = 0; i < lstFile.Count(); i++)
                {
                    var fi = new FileInfo(lstFile.ElementAt(i));

                    lst.Add(new FileInfoModel() { FileName = fi.Name, ContentLength = fi.Length, Path = fi.FullName.Replace(appPath, "/").Replace("\\", "/") });
                }

                return new JsonResult() { Data = lst };
            }

            return new EmptyResult();
        }

        [HttpPost]
        public ActionResult CreateFolder(string folderName, string parent)
        {
            var strPath = Server.MapPath(Path.Combine(parent, folderName));

            if (!Directory.Exists(strPath))
            {
                Directory.CreateDirectory(strPath);
            }

            return new EmptyResult();
        }

        [HttpPost]
        public ActionResult Upload(IEnumerable<HttpPostedFileBase> attachments, string path)
        {
            if (attachments != null)
            {
                foreach (var file in attachments)
                {
                    // Some browsers send file names with full path. We only care about the file name.
                    var fileName = Path.GetFileName(file.FileName);

                    var fileExtension = Path.GetExtension(file.FileName);

                    string uploadFolder = path;

                    if (string.IsNullOrWhiteSpace(path))
                    {
                        uploadFolder = "~/Upload";

                        switch (fileExtension)
                        {
                            case ".jpg":
                            case ".png":
                            case ".gif":
                                uploadFolder = "~/Upload/Image";
                                break;
                            case ".mp3":
                                uploadFolder = "~/Upload/Audio";
                                break;
                        }
                    }

                    var destinationPath = Path.Combine(Server.MapPath(uploadFolder), fileName);


                    file.SaveAs(destinationPath);
                }
            }

            // Return an empty string to signify success
            return Content("");
        }

        public ActionResult RemoveUpload(string[] fileNames, string path)
        {
            //if (string.IsNullOrWhiteSpace(fileNames))
            //{
            //    return new JavaScriptResult() { Script = "alert(\"Tên file không được để trống.\")" };
            //}

            return Content("");
        }

        private string GetFolder(string file)
        {
            return string.Empty;
        }
    }
}
